<template>

	<view class="login-box">
		<uni-icons type="contact-filled" size="100" color="gray"></uni-icons>
		<view class="login-btn" @click="login">一键登录</view>
		<text>登陆后享更多权益 </text>
	</view>

</template>

<script>
	import {
		mapMutations,
		mapState
	} from 'vuex'
	export default {
		name: "my-login",
		data() {
			return {

			};
		},
		computed: {
			...mapState('m_user', ['redirectInfo'])
		},
		methods: {
			...mapMutations('m_user', ['updateUserInfo', 'updateToken','updateRedirectInfo']),
			async login() {
				// 获取用户信息
				const [err, res] = await uni.getUserProfile({
					desc: '登陆'
				})
				if (err !== null) return uni.$showMsg('获取用户信息失败，请重试')
				// console.log(res);
				this.updateUserInfo(res.userInfo)
				// 调用登陆接口
				const [err1, res1] = await uni.login()
				// console.log(res1);
				if (err1 !== null) return $showMsg('登陆失败')
				// 准备参数
				const params = {
					encryptedData: res.encryptedData,
					rawData: res.rawData,
					iv: res.iv,
					signature: res.signature,
					code: res1.code
				}
				// console.log(params)
				// 换取token
				// const {
				// 	data: {
				// 		message,
				// 		meta
				// 	}
				// } = await uni.$http.post('/api/public/v1/users/wxlogin', params)
				this.updateToken('mengdididididi12345')
				// if (meta.status !== 200) return uni.$showMsg()
				// console.log(message)

				// 自动跳转回购物车页面 不是所有的登陆都自动跳转 只有在购物车页面结算时跳转到登陆页面 才需要跳转回去
				if (Object.keys(this.redirectInfo).length !== 0) {
					let param = {
						url: this.redirectInfo.from,
						success: () => {
							// 清空redirectInfo
							this.updateRedirectInfo({})
						}
					}
					// 判断返回到类型
					if (this.redirectInfo.type === 'switchTab') {
						uni.switchTab(param)
					} else {
						uni.navigateTo(param)
					}
				}

			}
		}
	}
</script>

<style lang="scss">
	.login-box {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 100px;

		.login-btn {
			width: 90%;
			line-height: 50px;
			text-align: center;
			color: #fff;
			background-color: #ea4350;
			border-radius: 30px;
			margin: 15px 0;
		}

		text {
			font-size: 12px;
			color: gray;
		}
	}
</style>
